package com.bawei.hgshop.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bawei.hgshop.common.EsUtil;
import com.bawei.hgshop.entity.Category;
import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.entity.Sku;
import com.bawei.hgshop.entity.Spec;
import com.bawei.hgshop.entity.SpecOption;
import com.bawei.hgshop.entity.Spu;
import com.bawei.hgshop.entity.SpuVo;
import com.bawei.hgshop.service.CategoryService;
import com.bawei.hgshop.service.SkuService;
import com.bawei.hgshop.service.SpecService;
import com.bawei.hgshop.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
public class IndexController  {
	
	// 注入redis
	@Autowired
	RedisTemplate<String, Category> catRedisTmp;
	
	// 分类的服务 
	@Reference
	CategoryService catService;
	
	//商品的服务
	@Reference
	SpuService spuService; 
	
	
	//规格的服务
	@Reference
	SpecService specService;
	
	//规格的服务
		@Reference
		SkuService skuService;
		
	/**
	 * 注入es 查询的模板	
	 */
	@Autowired
	ElasticsearchTemplate esTemplate;
		
	
	
	
	/**
	 * 首页
	 * @param request
	 * @return
	 */
	@RequestMapping("/")
	public String index(HttpServletRequest request) {
		
		// 如果有redis 则直接从redis 获取并使用
		if(catRedisTmp.hasKey("cattree")) {
			// 获取所有的分类
			List<Category> catList = catRedisTmp.opsForList().range("cattree", 0, -1);
			request.setAttribute("cattree", catList);
		}else {
			// 如果没有，则从service 当中获取
			List<Category> catList = catService.tree();
			// 获取到以后，放入到redis 当中
			catRedisTmp.opsForList().leftPushAll("cattree", catList);
			//设置过期时间
			//catRedisTmp.expire("cattree", 1000, TimeUnit.SECONDS);// 1000秒以后过期
			//一个小时
			catRedisTmp.expire("cattree", 60*60 ,TimeUnit.SECONDS);// 一小时秒以后过期
			
		}
		
		return "index";
	}
	
	@RequestMapping("search")
	public String search(HttpServletRequest request, String key,
			@RequestParam(defaultValue = "1") int page ,
			@RequestParam(defaultValue = "3")  int pageSize) {
		
		 EsUtil<Spu> esUtil = new EsUtil<Spu>(esTemplate);
	
			
		 PageInfo<Spu> pageInfo = esUtil.myHlQuery(page, pageSize, key, new String[] {"goodsName","caption","smallPic"},
				 new String[] {"goodsName","caption"},  new String[] {"id","goodsName"}, Spu.class);
		 request.setAttribute("pageInfo", pageInfo);
		 request.setAttribute("key", key);
		 
		return "search";
	}
	
	/**
	 * 获取这个分类下边的spu
	 * @param request
	 * @param catId
	 * @return
	 */
	@RequestMapping("shop")
	public String shop(HttpServletRequest request,int catId) {
		SpuVo spuVo = new SpuVo();
		spuVo.setPage(1);
		spuVo.setPageSize(8);
		//  要查询的分类
		Category category = new Category();
		category.setId(catId);
		spuVo.setCategory(category);
		
		
		//得到页面要显示的spu列表
		PageInfo<Spu> pageInfo = spuService.list(spuVo);
		request.setAttribute("pageInfo", pageInfo);
		pageInfo.getList().forEach(x->{
			System.out.println(x);
		});
		
		return "shop";
	}
	
	/**
	 * 商品详情
	 * @param request
	 * @param spuId
	 * @return
	 */
	@RequestMapping("detail")
	public String detail(HttpServletRequest request,int spuId) {
		// 仅仅是spu 但是没有获取sku
		Spu spu = spuService.getById(spuId);
		
		// 获取该商品所有的规格以及规格属性
		// select * from hg_spec WHERE id in   (  select    DISTINCT  spec_id   from hg_sku_spec where sku_id in (   select id from hg_sku WHERE spu_id=218) )
		List<Spec>  specs =  specService.listBySpuId(spuId);
		request.setAttribute("specs", specs);
		request.setAttribute("spu", spu);
		
		return "item";
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getSku")  
	@ResponseBody
	public MsgResult getSku(HttpServletRequest request,@RequestBody SpuVo spuVo) {
		
		List<SpecOption> list = spuVo.getOptions(); //Arrays.asList(spuVo.getOptions());
		
		list.forEach(x->{
			System.out.println("x is " + x);
		});
		
		Sku sku=  skuService.listBySpec(spuVo);
		if(sku==null) {
			//没有找到对应的元素
			return new MsgResult(1, "", "");
		}else {
			// 找到对应的sku
			return new MsgResult(0, "", sku);
		}
		
	}
	

}
